package mysql

import (
	"fmt"
	"gorm.io/driver/mysql"
	"gorm.io/gorm"
	"userprofile/appconfig"
)

var err error
var DB *gorm.DB

func MysqlInit() {
	Data := appconfig.Data.Mysql
	dsn := fmt.Sprintf("%s:%s@tcp(%s:%d)/%s?charset=utf8mb4&parseTime=True&loc=Local", Data.User, Data.Password, Data.Host, Data.Port, Data.Database)
	DB, err = gorm.Open(mysql.Open(dsn), &gorm.Config{})

	if err != nil {
		panic(err)
	} else {
		fmt.Println("数据库连接成功")
	}
	err = DB.Debug().AutoMigrate(&User{}, &Account{})
	if err != nil {
		panic(err)
	} else {
		fmt.Println("数据库迁移成功")
	}

}
